package net.wanglu.www.zzz.rule.artcl;

import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.np._NPTuple;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;

public class Nationality extends _ArticleRule {

	public final Pattern Nationality = Pattern.compile("Afghan\\b|\\bAlbanian\\b|\\bAlgerian\\b|\\bAlldorran\\b|\\bAngnlall\\b|\\bAlltiguan\\b|\\bArmenian\\b|\\bAustralian\\b|\\bAustrian\\b|\\bBahamian\\b|\\bBallraini\\b|\\bBangladeshi\\b|\\bBarbadian\\b|\\bBelorussian\\b|\\bBelgian\\b|\\bBelizean\\b|\\bBeninese\\b|\\bBermudan\\b|\\bBhutanese\\b|\\bBolivian\\b|\\bBosnian\\b|\\bBotswanan\\b|\\bBrazilian\\b|\\bBruneian\\b|\\bBulgarian\\b|\\bBurkinan\\b|\\bBurundian\\b|\\bCambodian\\b|\\bCameroonian\\b|\\bCanadian\\b|\\bCape Verdean\\b|\\bCaribbean\\b|\\bCentral African\\b|\\bChadian\\b|\\bChilean\\b|\\bChinese\\b|\\bColombian\\b|\\bCongolese\\b|\\bCosta Rican\\b|\\bIvorian\\b|\\bCuban\\b|\\bCypriot\\b|\\bCzech\\b|\\bDjiboutian\\b|\\bDominican\\b|\\bEast Timorese\\b|\\bEcuadorean\\b|\\bEgyptian\\b|\\bSalvadorean\\b|\\bEquatorial Guinean\\b|\\bEritrean\\b|\\bEstonian\\b|\\bEthiopian\\b|\\bFijian\\b|\\bGabonese\\b|\\bGambian\\b|\\bGeorgian\\b|\\bGreman\\b|\\bGhanaian\\b|\\bGibraltarian\\b|\\bGreek\\b|\\bGrenadian\\b|\\bGuatemalan\\b|\\bGuinean\\b|\\bGuinea-Bissauan\\b|\\bGuvanese\\b|\\bHaitian\\b|\\bHonduran\\b|\\bHungarian\\b|\\bIndian\\b|\\bIndonesian\\b|\\bIranian\\b|\\bIraqi\\b|\\bIsraeli\\b|\\bItalian\\b|\\bJamaican\\b|\\bJapanese\\b|\\bJordanian\\b|\\bKazakh\\b|\\bKenyan\\b|\\bKiribati\\b|\\bNorth Korean\\b|\\bSouth Korean\\b|\\bKuwaiti\\b|\\bKyrgyz\\b|\\bLaotian\\b|\\bLatvian\\b|\\bLebanese\\b|\\bLiberian\\b|\\bLibyan\\b|\\bLiechtenstein\\b|\\bLithuanian\\b|\\bMalagasy\\b|\\bMalawian\\b|\\bMalaysian\\b|\\bMaldivian\\b|\\bMalian\\b|\\bMaltese\\b|\\bMauritanian\\b|\\bMauritian\\b|\\bMelanesian\\b|\\bMexican\\b|\\bMicronesian\\b|\\bMoldovan\\b|\\bMonegasque\\b|\\bMongolian\\b|\\bMontserratian\\b|\\bMoroccan\\b|\\bMozambican\\b|\\bBurmese\\b|\\bNamibian\\b|\\bNauruan\\b|\\bNepalese\\b|\\bNicaraguan\\b|\\bNigerien\\b|\\bNorwegian\\b|\\bOmani\\b|\\bPakistani\\b|\\bPanamanian\\b|\\bPapua New Guinean\\b|\\bParaguayan\\b|\\bPeruvian\\b|\\bPolish\\b|\\bPolynesian\\b|\\bPortuguese\\b|\\bPuerto Rican\\b|\\bQatari\\b|\\bRomanian\\b|\\bRussian\\b|\\bRwandan\\b|\\bKittitian\\b|\\bNevisian\\b|\\bSaint Lucian\\b|\\bVincentian\\b|\\bSamoan\\b|\\bSanmarinese\\b|\\bSao Tomean\\b|\\bSaudi Arabian\\b|\\bSaudi\\b|\\bScandinavian\\b|\\bSenegalese\\b|\\bSeychellois\\b|\\bSierra Leonean\\b|\\bSingaporean\\b|\\bSlovak\\b|\\bSlovakian\\b|\\bSlovene\\b|\\bSlovenian\\b|\\bSomali\\b|\\bSouth African\\b|\\bSri Lankan\\b|\\bSudanese\\b|\\bSwazi\\b|\\bSwiss\\b|\\bSyrian\\b|\\bTajik\\b|\\bTanzanian\\b|\\bThai\\b|\\bTogolese\\b|\\bTongan\\b|\\bTrinidadian\\b|\\bTobagan\\b|\\bTunisian\\b|\\bTurkmen\\b|\\bTuvaluan\\b|\\bUgandan\\b|\\bUkrainian\\b|\\bEmirati\\b|\\bUruguayan\\b|\\bUzbek\\b|\\bVanuatuan\\b|\\bVatican\\b|\\bVenezuelan\\b|\\bVietnamese\\b|\\bSahrawian\\b|\\bYemeni\\b|\\bZambian\\b|\\bZimbabwean\\b|\\bArgentinian\\b|\\bAzeri\\b|\\bAzerbaijani\\b|\\bCayman Islander\\b|\\bCroat\\b|\\bDane\\b|\\bEnglishman\\b|\\bFinn\\b|\\bFrenchman\\b|\\bBriton\\b|\\bGreenlander\\b|\\bIcelander\\b|\\bMosotho\\b|\\bLiechtensteiner\\b|\\bLuxembourger\\b|\\bMarshall Islander\\b|\\bDutchman\\b|\\bNew Zealander\\b|\\bIrishman\\b|\\bFilipino\\b|\\bPole\\b|\\bIrishman\\b|\\bScot\\b|\\bSerb\\b|\\bMontenegram\\b|\\bSolomon Islander\\b|\\bSpaniard\\b|\\bSurinamer\\b|\\bSwede\\b|\\bTurk\\b|\\bBriton\\b|\\bWelshman");
	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();
		if ( NP.depth() != 2) return Rule.Inapplicable;//如果不是叶子节点的父母则不符合要求
		TaggedWord headword = npt.getHeadword();
		if ( "NN".equals(headword.tag()) || "NNP".equals(headword.tag())) {
			return Rule.Applicable;//如果中心词是名词则符合要求，否则不符合
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( this.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();

		/*
		 * 名词短语中的headword
		 */
		TaggedWord headword = npt.getHeadword();
		if ( !this.Nationality.matcher(headword.word()).find()) return Rule.Inapplicable;
		
		TregexPattern pattern = null;
		TregexMatcher matcher = null;

		/*
		 * 名词短语中含有的冠词，也可能没有
		 */
		Tree dt = null;
		sb.clear();
		sb.append("/\\bDT\\b/");
		pattern = TregexPattern.compile(sb.toString());
		matcher = pattern.matcher(NP);
		if ( matcher.find()) {
			dt = matcher.getMatch();
		}

		if ( dt == null) {
			BadEgg egg = new BadEgg();
			egg.whereIsWrong = NP;
			egg.setProperty(BadEgg.Error);
			egg.setViolatedRule(this);
			return egg;									
		} else {
			String dtWord = dt.taggedYield().get(0).word();
			if( "the".equalsIgnoreCase(dtWord)) {
				return null;
			} else if ( "a".equalsIgnoreCase(dtWord) || "an".equalsIgnoreCase(dtWord)) {
				BadEgg egg = new BadEgg();
				egg.whereIsWrong = NP;
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				return egg;					
			} else {
				return Rule.Inapplicable;
			}
		}
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.append("<p>");
		sb.append("定冠词可以和某些名词连用，表示一个民族。");
		sb.append("</p>");			
		return sb.toString();
	}
}
